package com.collect.zambia.zhiHelper;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.net.Uri;

import android.text.TextUtils;

import com.collect.zambia.zhiUtils.ToastUtil;

import java.net.URLEncoder;

public class ZHISysShareHelper {

    public static void sendEmail(Context context, String email, String title) {
        if (TextUtils.isEmpty(email)) {
            return;
        }
        Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:" + email));
        intent.putExtra(Intent.EXTRA_TEXT, title);
        context.startActivity(Intent.createChooser(intent, title));
    }

    public static void shareOnBrowser(Context context, String url) {
        if (TextUtils.isEmpty(url)) {
            return;
        }
        Uri uri = Uri.parse(url);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } else {
            context.startActivity(Intent.createChooser(intent, "Please select a browser to open"));
        }
    }

    public static void callPhone(Context context, String phone) {
        if (TextUtils.isEmpty(phone)) {
            return;
        }

        phone = "0" + phone;

        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phone);
        intent.setData(data);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    public static void callPhone(Context context, String phone, Boolean needZeroPrifix) {
        if (TextUtils.isEmpty(phone)) {
            return;
        }

        if (needZeroPrifix) {
            phone = "0" + phone;
        }

        Intent intent = new Intent(Intent.ACTION_DIAL);
        phone = Uri.encode(phone);

        Uri data = Uri.parse("tel:" + phone);
        intent.setData(data);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    public static void shareWhatsApp(Context context, String msg) {
        final String prefixUrl = "https://wa.me/";
        final String WHATS_APP = "com.whatsapp";
        final String WHATS_APP_BA = "com.whatsapp.w4b";
        String targetPackage = "";

        try {
            if (isAppInstall(context, WHATS_APP)) {
                targetPackage = WHATS_APP;
            } else if (isAppInstall(context, WHATS_APP_BA)) {
                targetPackage = WHATS_APP_BA;
            }

            if (TextUtils.isEmpty(targetPackage)) {
                ToastUtil.s("You haven't installed whatsAPP");
                return;
            }

            String gUrl = (prefixUrl + msg);
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_VIEW);
            sendIntent.setData(Uri.parse(gUrl));
            sendIntent.setPackage(targetPackage);
            sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(sendIntent);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void sendMsgWithWhatsApp(Context context, String phone, String msg) {
        final String prefixUrl = "https://wa.me/";//"https://api.whatsapp.com/send?";
        final String WHATS_APP = "com.whatsapp";
        final String WHATS_APP_BA = "com.whatsapp.w4b";
        String targetPackage = "";
        try {
            if (isAppInstall(context, WHATS_APP)) {
                targetPackage = WHATS_APP;
            } else if (isAppInstall(context, WHATS_APP_BA)) {
                targetPackage = WHATS_APP_BA;
            }

            if (TextUtils.isEmpty(targetPackage)) {
                ToastUtil.s("You haven't installed whatsAPP");
                return;
            }

            String gUrl = (prefixUrl + phone);
            if (!TextUtils.isEmpty(msg)) {
                msg = URLEncoder.encode(msg, "UTF-8");
                gUrl = (gUrl + "?text=" + msg);
            }
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_VIEW);
            sendIntent.setData(Uri.parse(gUrl));
            sendIntent.setPackage(targetPackage);
            sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(sendIntent);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void openWhatsApp(Context context, String msg) {
        final String WHATS_APP = "com.whatsapp";
        final String WHATS_APP_BA = "com.whatsapp.w4b";
        String targetPackage = "";

        try {
            if (isAppInstall(context, WHATS_APP)) {
                targetPackage = WHATS_APP;
            } else if (isAppInstall(context, WHATS_APP_BA)) {
                targetPackage = WHATS_APP_BA;
            }

            if (TextUtils.isEmpty(targetPackage)) {
                ToastUtil.s("You haven't installed whatsAPP");
                return;
            }

            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, msg);
            sendIntent.setType("text/plain");
            sendIntent.setPackage(targetPackage);
            sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(sendIntent);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean isWhatsappInstall(Context context) {
        final String WHATS_APP = "com.whatsapp";
        final String WHATS_APP_BA = "com.whatsapp.w4b";

        return (isAppInstall(context, WHATS_APP) || isAppInstall(context, WHATS_APP_BA));
    }

    public static boolean isAppInstall(Context context, String appName) {
        PackageInfo packageInfo = null;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(appName, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return packageInfo != null;
    }

    public static void copyToClipboard(Context context, String copyStr) {
        try {
            //获取剪贴板管理器
            ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
            // 创建普通字符型ClipData
            ClipData mClipData = ClipData.newPlainText("Label", copyStr);
            // 将ClipData内容放到系统剪贴板里。
            cm.setPrimaryClip(mClipData);
            ToastUtil.s("Copy Success");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void copyToClipboard(Context context, String copyStr, boolean showToast) {
        try {
            //获取剪贴板管理器
            ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
            // 创建普通字符型ClipData
            ClipData mClipData = ClipData.newPlainText("Label", copyStr);
            // 将ClipData内容放到系统剪贴板里。
            cm.setPrimaryClip(mClipData);
            if (showToast) {
                ToastUtil.s("Copy Success");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
